package com.guigu.qingcheng.component;

import com.alibaba.fastjson.JSON;
import com.guigu.qingcheng.pojo.TbSku;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ElasticsearchIndex {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;
    /**
     * 索引库新增
     * @param skus
     */
    @RabbitListener(queues = "queue.indexUpdate")
    public void addIndex(List<TbSku> skus){
        try {
            //2.封装请求对象
            BulkRequest bulkRequest = new BulkRequest();
            for(TbSku sku: skus) {
                IndexRequest indexRequest = new IndexRequest("sku", "doc", sku.getId());
                Map skuMap = new HashMap<>();
                skuMap.put("name",sku.getName());
                skuMap.put("brandName",sku.getBrandName());
                skuMap.put("categoryName",sku.getCategoryName());
                skuMap.put("price",sku.getPrice());
                skuMap.put("image", sku.getImage());

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date createTime = sku.getCreateTime();
                if(createTime!=null)
                    skuMap.put("createTime", sdf.format(sku.getCreateTime()));
                else
                    skuMap.put("createTime", null);
                skuMap.put("saleNum", sku.getSaleNum());
                skuMap.put("commentNum", sku.getCommentNum());

                Map<String, String> spec = JSON.parseObject(sku.getSpec(), Map.class);
                skuMap.put("spec",spec);
                indexRequest.source(skuMap);
                bulkRequest.add(indexRequest);
            }


            //3.获取响应结果
            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            int status = bulkResponse.status().getStatus();
            System.out.println(status);
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 索引库删除
     * @param skus
     */
    @RabbitListener(queues = "queue.indexDelete")
    public void deleteIndex(List<TbSku> skus){
        try {
            BulkRequest bulkRequest = new BulkRequest();
            for(TbSku sku: skus){
                DeleteRequest deleteRequest = new DeleteRequest("sku", "doc", sku.getId());
                bulkRequest.add(deleteRequest);
            }
            BulkResponse bulk = client.bulk(bulkRequest,RequestOptions.DEFAULT);
            System.out.println(bulk.status().getStatus());
        } catch (IOException e) {
           e.printStackTrace();
        }
    }



}
